#pragma once
#include <iostream>
#include <string>
#include <jsoncpp/json/json.h>


//完整的报文： "len"\r\n"{  json串 }"\r\n
//\r\n不算做json串的一部分
//len为json串的长度

namespace protocol_ns
{
    const static std::string SEP = "\r\n";

    std::string EnCode(const std::string &in)
    {
        int message_length = in.size();
        std::string result = std::to_string(message_length);
        result += SEP;
        result += in;
        result += SEP;

        return result;
    }

    std::string DeCode(std::string& in)
    {
        auto pos = in.find(SEP);
        //一定不是一个完整的报文
        if(pos == std::string::npos)
        {
            return "";
        }
        //一定有完整的长度
        // len\r\n
        // len\r\n{   
        // len\r\n{    }
        // len\r\n{    }\r\n
        // len\r\n{    }\r\nlen
        std::string message_length_str = in.substr(0, pos);
        int message_length = std::stoi(message_length_str);
        int total_length = message_length_str.size() + message_length + 2 * SEP.size();
        //一定不是一个完整的报文
        if(in.size() < total_length)
            return "";
        
        std::string result = in.substr(pos + SEP.size(), message_length);
        in.erase(0, total_length);

        return result;
    }

    class Request
    {
    public:
        Request(int x = 0, int y = 0, char oper = 0)
            :_x(x), _y(y), _oper(oper)
        {
        }
        bool Serialize(std::string &out)
        {
            Json::Value root;
            Json::FastWriter writer;

            root["x"] = _x;
            root["y"] = _y;
            root["oper"] = _oper;

            out = writer.write(root);
            return true;
        }
        bool DeSerialize(const std::string &in)
        {
            Json::Value root;
            Json::Reader reader;

            bool res = reader.parse(in, root);

            if (res)
            {
                _x = root["x"].asInt();
                _y = root["y"].asInt();
                _oper = root["oper"].asInt();
            }
            return res;
        }
        ~Request()
        {
        }

    public:
        int _x;
        int _y;
        char _oper;
    };

    class Response
    {
    public:
        Response(int res = 0, int code = 0)
            :_result(res), _code(code)
        {
        }
        bool Serialize(std::string &out)
        {
            Json::Value root;
            Json::FastWriter writer;

            root["result"] = _result;
            root["code"] = _code;

            out = writer.write(root);
            return true;
        }
        bool DeSerialize(const std::string &in)
        {
            Json::Value root;
            Json::Reader reader;

            bool res = reader.parse(in, root);

            if (res)
            {
                _result = root["result"].asInt();
                _code = root["code"].asInt();
            }
            return res;
        }
        ~Response()
        {
        }

    public:
        int _result;
        int _code;
    };
}